package io.wdb.modules.generator.generator.controller.client;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import io.wdb.common.annotation.SysLog;
import io.wdb.common.utils.PageUtils;
import io.wdb.common.utils.Query;
import io.wdb.common.utils.R;
import io.wdb.modules.generator.generator.controller.api.BaseController;
import io.wdb.modules.generator.generator.entity.AccountCashEntity;
import io.wdb.modules.generator.generator.entity.UserInfoEntity;
import io.wdb.modules.generator.generator.entity.customer.AccountCashLogCustomer;
import io.wdb.modules.generator.generator.entity.customer.UserInfoEntityCustomer;
import io.wdb.modules.generator.generator.entity.web.BaseResponse;
import io.wdb.modules.generator.generator.entity.web.ResponseEnum;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.client.ClientCapitalService;

/**
 * 
 * @ClassName: ClientCapitalController.java
 * @Description:客户资金管理
 * @Time 2017年12月14日
 * @author: Tom
 */
@Controller
@RequestMapping("/client/capital")
public class ClientCapitalController extends BaseController{
	
	
	private Logger logger = LoggerFactory.getLogger(ClientCapitalController.class);
	
	@Autowired
	private ClientCapitalService clientCapitalService;
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("client:capital:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<UserInfoEntityCustomer> list = clientCapitalService.queryList(query);
		int total = clientCapitalService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil).put("code", "");
	}
	/**
	 * 
	 * @Method:record
	 * @Description:个人资金流水
	 * @Time 2017年12月15日 下午1:53:52
	 * @author: Tom
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/record")
	@RequiresPermissions("client:capital:list")
	public R record(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<AccountCashLogCustomer> list = clientCapitalService.queryRecord(query);
		System.out.println(JSON.toJSONString(list));
		int total = clientCapitalService.queryTotalRecord(query);
		
		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:getUserCashLog
	 * @Description:个人资金入账和支出
	 * @Time 2017年12月14日 下午5:31:10
	 * @author: Tom
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cashlog")
	@RequiresPermissions("client:capital:list")
	public Map<String, BigDecimal> getUserCashLog(@RequestBody UserInfoEntity infoEntity) {
		Map<String,BigDecimal> results = clientCapitalService.getCash(infoEntity.getId());
		return results;
	}
	/**
	 * 
	 * @Method:addDeposit
	 * @Description:用户冲余额
	 * @Time 2017年12月14日 下午5:31:56
	 * @author: Tom
	 * @return 
	 */
	@SysLog("用户充余额")
	@ResponseBody
	@RequestMapping("/addCashAmount")
	@RequiresPermissions("client:capital:addcash")
	public BaseResponse addCashAmount(@RequestBody AccountCashEntity accountCashEntity) {
		BaseResponse baseResponse = success();
		try {
			clientCapitalService.addCashAmount(accountCashEntity);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:applyForRefund
	 * @Description:申请退押金
	 * @Time 2017年12月15日 下午2:47:51
	 * @author: Tom
	 * @param infoEntity
	 * @return 
	 */
	@SysLog("申请退押金")
	@ResponseBody
	@RequestMapping("/applyForRefund")
	@RequiresPermissions("client:capital:allpyforrefund")
	public BaseResponse applyForRefund(@RequestBody UserInfoEntity infoEntity) {
		BaseResponse baseResponse = success();
		try {
			WebResponse response = new WebResponse();
			int status = clientCapitalService.applyForRefund(infoEntity);
			if(status==1) {
				response.setCode(ResponseEnum.DEPOSIT_NOT_EXIST.getCode());
				response.setMessage(ResponseEnum.DEPOSIT_NOT_EXIST.getMessage());
				baseResponse.setDetail(response);
			}else if(status ==2){
				response.setCode(ResponseEnum.USER_FREEZE.getCode());
				response.setMessage(ResponseEnum.USER_FREEZE.getMessage());
				baseResponse.setDetail(response);
			}else if(status==3) {
				response.setCode(ResponseEnum.USER_CHARGE_LOG_NOT_EXIST.getCode());
				response.setMessage(ResponseEnum.USER_CHARGE_LOG_NOT_EXIST.getMessage());
				baseResponse.setDetail(response);
			}
			return baseResponse;
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:auditRefundDeposit
	 * @Description:审核退款
	 * @Time 2017年12月15日 下午3:48:57
	 * @author: Tom
	 * @return 
	 */
	@SysLog("审核退押金")
	@ResponseBody
	@RequestMapping("/auditRefundDeposit")
	@RequiresPermissions("client:capital:auditrefund")
	public BaseResponse auditRefundDeposit(@RequestBody UserInfoEntity infoEntity) {
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = clientCapitalService.auditRefundDeposit(infoEntity);
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:operateRefundDeposit
	 * @Description:操作退款
	 * @Time 2017年12月21日 下午3:34:05
	 * @author: Tom
	 * @param infoEntity
	 * @return
	 */
	@SysLog("操作退押金")
	@ResponseBody
	@RequestMapping("/operateRefundDeposit")
	@RequiresPermissions("client:capital:operaterefund")
	public BaseResponse operateRefundDeposit(@RequestBody UserInfoEntity infoEntity){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = clientCapitalService.operateRefundDeposit(infoEntity);
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:addCashAmount
	 * @Description:冲押金
	 * @Time 2017年12月21日 下午2:33:38
	 * @author: Tom
	 * @param infoEntity
	 * @return
	 */
	@SysLog("用户冲押金")
	@ResponseBody
	@RequestMapping("/addDepositAmount")
	@RequiresPermissions("client:capital:adddepositamount")
	public BaseResponse addDepositAmount(@RequestBody AccountCashEntity accountCashEntity) {
		BaseResponse baseResponse = success();
		try {
			//TODO
			clientCapitalService.addDepositAmount(accountCashEntity);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:getRefundLogs
	 * @Description:查看退款记录
	 * @Time 2017年12月21日 下午2:32:59
	 * @author: Tom
	 * @param params
	 * @return
	 */
	@Deprecated
	public R getRefundLogs(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<AccountCashLogCustomer> list = clientCapitalService.queryRecord(query);
		System.out.println(JSON.toJSONString(list));
		int total = clientCapitalService.queryTotalRecord(query);
		
		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	
}
